//
//  USTarotCoverController.m
//  us
//
//  Created by karry on 2025/5/19.
//

#import "USTarotCoverController.h"
#import "USTarotCoverCell.h"
#import "USTarotCard.h"

@interface USTarotCoverController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *colletionView;
@property (nonatomic,strong) NSArray *list;

@property (nonatomic,strong) NSString *coverName;

@end

@implementation USTarotCoverController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改卡背";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_gouzi"] style:UIBarButtonItemStylePlain target:self action:@selector(doneAction:)];

    
    CGFloat margin = 20;
    
    NSInteger count = 3;
    
    CGFloat itemW = (self.view.jk_width - margin * (count + 1))/count;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(floorf(itemW), itemW * 12/7.f + 40);
    layout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    
    self.colletionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.colletionView.dataSource = self;
    self.colletionView.backgroundColor = UIColor.clearColor;
    [self.colletionView registerNib:[UINib nibWithNibName:@"USTarotCoverCell" bundle:nil] forCellWithReuseIdentifier:@"USTarotCoverCellId"];
    self.colletionView.delegate = self;
    [self.view addSubview:self.colletionView];
    
    [self.colletionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.list = [USTarotCard coverList];
    
    [self.colletionView reloadData];
    
    dispatch_async(dispatch_get_main_queue(), ^{
       
        [self seletedDefaultName];
        
    });
    
}

- (void)startUpload{
    [self.indicator startAnimating];
    UIBarButtonItem *loadingItem = [[UIBarButtonItem alloc] initWithCustomView:self.indicator];
    self.navigationItem.rightBarButtonItem = loadingItem;
    self.view.userInteractionEnabled = NO; // 禁用整个页面操作

    
}
- (void)endUpLoad{
    self.navigationItem.rightBarButtonItem = self.doneItem;
    [self.indicator stopAnimating];
    self.view.userInteractionEnabled = YES;
}

- (void)doneAction:(id)sender{
    [super doneAction:sender];
    [USFeedbackTool impactSoft];
    
    
    [self startUpload];
    
    NSDictionary *params = @{
        @"tarotCove" : self.coverName ?:@"taluo_cover_0"
    };

    [[EMNetWork shareManager] em_post:API_EditUser
                               params:params
                                  hud:nil
                           showError:YES
                              success:^(EMApiModel *api) {
        [self.view endEditing:YES];
        [self endUpLoad];

        if (!api.success) {
            return;
        }
        USUser *user = [USUser user];
        user.tarotCove = self.coverName ?:@"taluo_cover_0";
        [USUser saveUser:user];
 
        [self.navigationController popViewControllerAnimated:YES];
        
    } fail:^{
        [self endUpLoad];

    }];
}
- (void)seletedDefaultName{
    
    NSString *imgName = USUser.user.tarotCove.length>0 ? USUser.user.tarotCove : @"taluo_cover_0";
    NSIndexPath *indexPath = nil;
    for (NSInteger i = 0; i < self.list.count; i++) {
        NSString *name = self.list[i];
        if ([name isEqualToString:imgName]) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }
    if (indexPath) {
        [self.colletionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionTop];
    }
    
}

#pragma mark - <UICollectionViewDataSource,UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [USFeedbackTool selectionChanged];
    
    NSString *name = self.list[indexPath.row];
    
    self.coverName = name;
 
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    USTarotCoverCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"USTarotCoverCellId" forIndexPath:indexPath];
    
    cell.imageV.image = [UIImage imageNamed:self.list[indexPath.row]];
    
    return cell;
    
}



@end
